package com.inputabc.ct.v1.ui.listener;

import com.inputabc.EzGUIFramework.util.EzGUI;
import com.inputabc.ct.v1.context.Components;
import com.inputabc.ct.v1.context.ComponentsBuilder;
import com.inputabc.ct.v1.context.ContextParams;
import com.inputabc.ct.v1.controller.TextContentController;
import com.inputabc.ct.v1.setting.SettingContext;
import com.inputabc.ct.v1.setting.TranslateSetting;
import com.inputabc.ct.v1.ui.TextBox;
import com.inputabc.ct.v1.ui.inputbox.InputBoxBuilder;
import com.inputabc.ct.v1.ui.keysetting.KeySettingBuilder;
import com.inputabc.ct.v1.ui.keysetting.component.KeySettingFrame;
import com.inputabc.ct.v1.util.BaiduUtils;
import com.inputabc.ct.v1.util.GoogleUtils;
import com.inputabc.ct.v1.util.TTSUtils;
import com.inputabc.ct.v1.util.YoudaoUtils;
import com.voicerss.tts.Languages;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

public class TextBoxPopupMeunListenerHandler {
    private JPopupMenu jpm;
    private Components textBoxComponents = ComponentsBuilder.getComponentsContext().get(TextBox.class);
    private JFrame jf = (JFrame) textBoxComponents.get("textBoxFrame");
    private JTextArea jta = (JTextArea) textBoxComponents.get("textBoxTextArea");
    private JScrollPane jsp = (JScrollPane) textBoxComponents.get("textBoxScrollPane");
    private Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
    private Map<Object, Object> contextParam = ContextParams.contextParam;
    private FSDirectory fsdir;
    private TextContentController tcc = new TextContentController();
    private TranslateSetting translateSetting = SettingContext.translateSetting;
    private InputBoxBuilder inputBoxBuilder = (InputBoxBuilder) ContextParams.contextParam.get("inputBoxBuilder");
    public TextBoxPopupMeunListenerHandler() {
        try {
            fsdir = FSDirectory.open((Path) contextParam.get("indexPath"));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public TextBoxPopupMeunListenerHandler(JPopupMenu jpm) {
        this();
        this.jpm = jpm;
    }

    public void bindMenuItemListener() {
        Component[] components = jpm.getComponents();
        for (Component c : components) {
            if (c instanceof JMenuItem) {
                if ("textBoxClearMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> jta.setText(""));
                }
                if ("textBoxOpenInputBoxMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> inputBoxBuilder.build());
                }
                if ("textBoxCloseMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        EzGUI.fadeOut(jf, EzGUI.FADE_FAST);
                        System.exit(0);
                    });
                }
                if ("textBoxSelectAllMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> jta.select(0, jta.getText().length()));
                }
                if ("textBoxCopyMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        String selectedText = jta.getSelectedText();
                        contextParam.put("previousText", jta.getText());
                        StringSelection stringSelection = new StringSelection(selectedText);
                        clip.setContents(stringSelection, null);

                    });
                }

                if ("textBoxSelectAllAndCopyMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        jta.select(0, jta.getText().length());
                        String selectedText = jta.getSelectedText();
                        contextParam.put("previousText", jta.getText());
                        StringSelection stringSelection = new StringSelection(selectedText);
                        clip.setContents(stringSelection, null);

                    });
                }
                if ("textBoxMoveMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> jta.setCursor(new Cursor(Cursor.MOVE_CURSOR)));
                }

                if ("textBoxRefreshMenu".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        JFrame jf = (JFrame) textBoxComponents.get("textBoxFrame");
                        jf.setVisible(false);
                        jf.setVisible(true);
                        JTextArea jta = (JTextArea) textBoxComponents.get("textBoxTextArea");
                        JPanel jp = (JPanel) textBoxComponents.get("textBoxPanel");
                        String text = jta.getText();
                        jta.setText("");
                        jp.updateUI();
                        jta.setText(text);
                        jta.updateUI();
                    });
                }
                if ("textBoxTranslationMenu".equals(c.getName())) {
                    final JMenu translateMenu = (JMenu) c;
                    final Component[] components2 = translateMenu.getMenuComponents();
                    for (Component c2 : components2) {
                        if ("textBoxTranslationMenuEN2CN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.ENGLISH);
                                    translateSetting.setTarget(YoudaoUtils.CHINESE);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.ENGLISH);
                                    translateSetting.setTarget(BaiduUtils.CHINESE);
                                }
                                else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.ENGLISH);
                                    translateSetting.setTarget(GoogleUtils.CHINESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuJA2CN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.JPANESE);
                                    translateSetting.setTarget(YoudaoUtils.CHINESE);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.JPANESE);
                                    translateSetting.setTarget(BaiduUtils.CHINESE);
                                } else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.JPANESE);
                                    translateSetting.setTarget(GoogleUtils.CHINESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuKR2CN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.KOREAN);
                                    translateSetting.setTarget(YoudaoUtils.CHINESE);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.KOREAN);
                                    translateSetting.setTarget(BaiduUtils.CHINESE);
                                } else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.KOREAN);
                                    translateSetting.setTarget(GoogleUtils.CHINESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuCN2EN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.CHINESE);
                                    translateSetting.setTarget(YoudaoUtils.ENGLISH);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.CHINESE);
                                    translateSetting.setTarget(BaiduUtils.ENGLISH);
                                }else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.CHINESE);
                                    translateSetting.setTarget(GoogleUtils.ENGLISH);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuCN2JA".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.CHINESE);
                                    translateSetting.setTarget(YoudaoUtils.JPANESE);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.CHINESE);
                                    translateSetting.setTarget(BaiduUtils.JPANESE);
                                } else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.CHINESE);
                                    translateSetting.setTarget(GoogleUtils.JPANESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuCN2KR".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if ("youdao".equals(engine)) {
                                    translateSetting.setSource(YoudaoUtils.CHINESE);
                                    translateSetting.setTarget(YoudaoUtils.KOREAN);
                                } else if ("baidu".equals(engine)) {
                                    translateSetting.setSource(BaiduUtils.CHINESE);
                                    translateSetting.setTarget(BaiduUtils.KOREAN);
                                }else if ("google".equals(engine)) {
                                    translateSetting.setSource(GoogleUtils.CHINESE);
                                    translateSetting.setTarget(GoogleUtils.KOREAN);
                                }
                                deleteIndexAll();

                            });
                        } else if ("textBoxTranslationMenuAUTO2CN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if("youdao".equals(engine)){
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                    translateSetting.setTarget(YoudaoUtils.CHINESE);
                                }else if("baidu".equals(engine)){
                                    translateSetting.setSource(BaiduUtils.AUTO);
                                    translateSetting.setTarget(BaiduUtils.CHINESE);
                                }else if("google".equals(engine)){
                                    translateSetting.setSource(GoogleUtils.AUTO);
                                    translateSetting.setTarget(GoogleUtils.CHINESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuAUTO2EN".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if("youdao".equals(engine)){
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                    translateSetting.setTarget(YoudaoUtils.ENGLISH);
                                }else if("baidu".equals(engine)){
                                    translateSetting.setSource(BaiduUtils.AUTO);
                                    translateSetting.setTarget(BaiduUtils.ENGLISH);
                                }else if("google".equals(engine)){
                                    translateSetting.setSource(GoogleUtils.AUTO);
                                    translateSetting.setTarget(GoogleUtils.ENGLISH);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuAUTO2JA".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if("youdao".equals(engine)){
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                    translateSetting.setTarget(YoudaoUtils.JPANESE);
                                }else if("baidu".equals(engine)){
                                    translateSetting.setSource(BaiduUtils.AUTO);
                                    translateSetting.setTarget(BaiduUtils.JPANESE);
                                }else if("google".equals(engine)){
                                    translateSetting.setSource(GoogleUtils.AUTO);
                                    translateSetting.setTarget(GoogleUtils.JPANESE);
                                }
                                deleteIndexAll();
                            });
                        } else if ("textBoxTranslationMenuAUTO2KR".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                JMenuItem s = (JMenuItem) e.getSource();
                                String engine = translateSetting.getEngine();
                                if("youdao".equals(engine)){
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                    translateSetting.setTarget(YoudaoUtils.KOREAN);
                                }else if("baidu".equals(engine)){
                                    translateSetting.setSource(BaiduUtils.AUTO);
                                    translateSetting.setTarget(BaiduUtils.KOREAN);
                                }else if("google".equals(engine)){
                                    translateSetting.setSource(GoogleUtils.AUTO);
                                    translateSetting.setTarget(GoogleUtils.KOREAN);
                                }
                                deleteIndexAll();
                            });
                        }
                    }
                }
                if ("textBoxTranslationEngineMenu".equals(c.getName())) {
                    JMenu translateMenu = (JMenu) c;
                    Component[] components2 = translateMenu.getMenuComponents();
                    for (Component c2 : components2) {
                        if ("textBoxTranslationEngineMenuYoudao".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                if ("youdao".equals(translateSetting.getEngine())) {
                                    return;
                                }
                                translateSetting.setEngine("youdao");
                                String sourceLanguage = translateSetting.getSource();
                                String targetLanguage = translateSetting.getTarget();
                                if (BaiduUtils.AUTO.equals(sourceLanguage)||GoogleUtils.AUTO.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                }else if (BaiduUtils.ENGLISH.equals(sourceLanguage)||GoogleUtils.ENGLISH.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.ENGLISH);
                                } else if (BaiduUtils.CHINESE.equals(sourceLanguage)||GoogleUtils.CHINESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.CHINESE);
                                } else if (BaiduUtils.JPANESE.equals(sourceLanguage)||GoogleUtils.JPANESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.JPANESE);
                                } else if (BaiduUtils.KOREAN.equals(sourceLanguage)||GoogleUtils.KOREAN.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.KOREAN);
                                } else if (BaiduUtils.AUTO.equals(sourceLanguage)||GoogleUtils.AUTO.equals(sourceLanguage)) {
                                    translateSetting.setSource(YoudaoUtils.AUTO);
                                }
                                if (BaiduUtils.CHINESE.equals(targetLanguage)||GoogleUtils.CHINESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(YoudaoUtils.CHINESE);
                                } else if (BaiduUtils.ENGLISH.equals(targetLanguage)||GoogleUtils.ENGLISH.equals(targetLanguage)) {
                                    translateSetting.setTarget(YoudaoUtils.ENGLISH);
                                } else if (BaiduUtils.JPANESE.equals(targetLanguage)||GoogleUtils.JPANESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(YoudaoUtils.JPANESE);
                                } else if (BaiduUtils.KOREAN.equals(targetLanguage)||GoogleUtils.KOREAN.equals(targetLanguage)) {
                                    translateSetting.setTarget(YoudaoUtils.KOREAN);
                                }

                                deleteIndexAll();
                                String sourceContent = (String) contextParam.get("sourceContent");
                                if (sourceContent != null) {
                                    try {
                                        String translatedContent = tcc.getTranslatedContent(sourceContent);
                                        contextParam.put("translatedContent", translatedContent);
                                        Map<Integer, Color> fgColorMap = (Map<Integer, Color>) ContextParams.contextParam
                                                .get("fgColorMap");
                                        int pos = (int) ContextParams.contextParam.get("colorMapPos");
                                        jta.setForeground(fgColorMap.get(pos));
                                        jta.setText(translatedContent);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                        if (jsp.getViewport().getBackground().getRGB() == -53971) {
                                            jta.setForeground(Color.BLUE);
                                        } else {
                                            jta.setForeground(Color.RED);
                                        }
                                        jta.setText("出错了！" + System.lineSeparator() + e1.getMessage());
                                    }
                                }
                            });
                        } else if ("textBoxTranslationEngineMenuBaidu".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                if ("baidu".equals(translateSetting.getEngine())) {
                                    return;
                                }
                                translateSetting.setEngine("baidu");
                                String sourceLanguage = translateSetting.getSource();
                                String targetLanguage = translateSetting.getTarget();
                                if(YoudaoUtils.AUTO.equals(sourceLanguage)||GoogleUtils.AUTO.equals(sourceLanguage)){
                                    translateSetting.setSource(BaiduUtils.AUTO);
                                }else if (YoudaoUtils.ENGLISH.equals(sourceLanguage)||GoogleUtils.ENGLISH.equals(sourceLanguage)) {
                                    translateSetting.setSource(BaiduUtils.ENGLISH);
                                } else if (YoudaoUtils.CHINESE.equals(sourceLanguage)||GoogleUtils.CHINESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(BaiduUtils.CHINESE);
                                } else if (YoudaoUtils.JPANESE.equals(sourceLanguage)||GoogleUtils.JPANESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(BaiduUtils.JPANESE);
                                } else if (YoudaoUtils.KOREAN.equals(sourceLanguage)||GoogleUtils.KOREAN.equals(sourceLanguage)) {
                                    translateSetting.setSource(BaiduUtils.KOREAN);
                                }
                                if (YoudaoUtils.ENGLISH.equals(targetLanguage)||GoogleUtils.ENGLISH.equals(targetLanguage)) {
                                    translateSetting.setTarget(BaiduUtils.ENGLISH);
                                } else if (YoudaoUtils.CHINESE.equals(targetLanguage)||GoogleUtils.CHINESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(BaiduUtils.CHINESE);
                                } else if (YoudaoUtils.JPANESE.equals(targetLanguage)||GoogleUtils.JPANESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(BaiduUtils.JPANESE);
                                } else if (YoudaoUtils.KOREAN.equals(targetLanguage)||GoogleUtils.KOREAN.equals(targetLanguage)) {
                                    translateSetting.setTarget(BaiduUtils.KOREAN);
                                }
                                deleteIndexAll();
                                String sourceContent = (String) contextParam.get("sourceContent");
                                if (sourceContent != null) {
                                    try {
                                        String translatedContent = tcc.getTranslatedContent(sourceContent);
                                        contextParam.put("translatedContent", translatedContent);
                                        Map<Integer, Color> fgColorMap = (Map<Integer, Color>) ContextParams.contextParam
                                                .get("fgColorMap");
                                        int pos = (int) ContextParams.contextParam.get("colorMapPos");
                                        jta.setForeground(fgColorMap.get(pos));
                                        jta.setText(translatedContent);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                        if (jsp.getViewport().getBackground().getRGB() == -53971) {
                                            jta.setForeground(Color.BLUE);
                                        } else {
                                            jta.setForeground(Color.RED);
                                        }
                                        jta.setText("出错了！" + System.lineSeparator() + e1.getMessage());
                                    }
                                }
                            });
                        }else if ("textBoxTranslationEngineMenuGoogle".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                if ("google".equals(translateSetting.getEngine())) {
                                    return;
                                }
                                translateSetting.setEngine("google");
                                String sourceLanguage = translateSetting.getSource();
                                String targetLanguage = translateSetting.getTarget();
                                if (YoudaoUtils.AUTO.equals(sourceLanguage)||BaiduUtils.AUTO.equals(sourceLanguage)) {
                                    translateSetting.setSource(GoogleUtils.AUTO);
                                } else if (YoudaoUtils.ENGLISH.equals(sourceLanguage)||BaiduUtils.ENGLISH.equals(sourceLanguage)) {
                                    translateSetting.setSource(GoogleUtils.ENGLISH);
                                } else if (YoudaoUtils.CHINESE.equals(sourceLanguage)||BaiduUtils.CHINESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(GoogleUtils.CHINESE);
                                } else if (YoudaoUtils.JPANESE.equals(sourceLanguage)||BaiduUtils.JPANESE.equals(sourceLanguage)) {
                                    translateSetting.setSource(GoogleUtils.JPANESE);
                                } else if (YoudaoUtils.KOREAN.equals(sourceLanguage)||BaiduUtils.KOREAN.equals(sourceLanguage)) {
                                    translateSetting.setSource(GoogleUtils.KOREAN);
                                }
                                if (YoudaoUtils.ENGLISH.equals(targetLanguage)||BaiduUtils.ENGLISH.equals(targetLanguage)) {
                                    translateSetting.setTarget(GoogleUtils.ENGLISH);
                                } else if (YoudaoUtils.CHINESE.equals(targetLanguage)||BaiduUtils.CHINESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(GoogleUtils.CHINESE);
                                } else if (YoudaoUtils.JPANESE.equals(targetLanguage)||BaiduUtils.JPANESE.equals(targetLanguage)) {
                                    translateSetting.setTarget(GoogleUtils.JPANESE);
                                } else if (YoudaoUtils.KOREAN.equals(targetLanguage)||BaiduUtils.KOREAN.equals(targetLanguage)) {
                                    translateSetting.setTarget(GoogleUtils.KOREAN);
                                }
                                deleteIndexAll();
                                String sourceContent = (String) contextParam.get("sourceContent");
                                if (sourceContent != null) {
                                    try {
                                        String translatedContent = tcc.getTranslatedContent(sourceContent);
                                        contextParam.put("translatedContent", translatedContent);
                                        Map<Integer, Color> fgColorMap = (Map<Integer, Color>) ContextParams.contextParam
                                                .get("fgColorMap");
                                        int pos = (int) ContextParams.contextParam.get("colorMapPos");
                                        jta.setForeground(fgColorMap.get(pos));
                                        jta.setText(translatedContent);
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                        if (jsp.getViewport().getBackground().getRGB() == -53971) {
                                            jta.setForeground(Color.BLUE);
                                        } else {
                                            jta.setForeground(Color.RED);
                                        }
                                        jta.setText("出错了！" + System.lineSeparator() + e1.getMessage());
                                    }
                                }
                            });
                        }
                    }
                }
                if ("textBoxSpeckMenu".equals(c.getName())) {
                    JMenu speckMenu = (JMenu) c;
                    Component[] menuComponents = speckMenu.getMenuComponents();
                    for (Component c2 : menuComponents) {
                        if ("textBoxSpeckMenuBeforeTranslate".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> {
                                String engine = translateSetting.getEngine();
                                String sourceLanguage = translateSetting.getSource();
                                String content = (String) contextParam.get("sourceContent");
                                if ("youdao".equals(engine)) {
                                    if (YoudaoUtils.CHINESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Chinese_China);
                                    } else if (YoudaoUtils.ENGLISH.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.English_GreatBritain);
                                    } else if (YoudaoUtils.JPANESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Japanese);
                                    } else if (YoudaoUtils.KOREAN.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Korean);
                                    }
                                } else if ("baidu".equals(engine)) {
                                    if (BaiduUtils.CHINESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Chinese_China);
                                    } else if (BaiduUtils.ENGLISH.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.English_GreatBritain);
                                    } else if (BaiduUtils.JPANESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Japanese);
                                    } else if (BaiduUtils.KOREAN.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Korean);
                                    }
                                }else if ("google".equals(engine)) {
                                    if (GoogleUtils.CHINESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Chinese_China);
                                    } else if (GoogleUtils.ENGLISH.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.English_GreatBritain);
                                    } else if (GoogleUtils.JPANESE.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Japanese);
                                    } else if (GoogleUtils.KOREAN.equals(sourceLanguage)) {
                                        TTSUtils.speak(content, Languages.Korean);
                                    }
                                }
                            });
                        } else if ("textBoxSpeckMenuAfterTranslatedMenu".equals(c2.getName())) {
                            JMenu afterTranslatedMenu = (JMenu) c2;
                            Component[] menuComponents2 = afterTranslatedMenu.getMenuComponents();
                            for (Component c3 : menuComponents2) {
                                if ("textBoxSpeckMenuAfterTranslateMenuAll".equals(c3.getName())) {
                                    ((JMenuItem) c3).addActionListener(e -> {
                                        String engine = translateSetting.getEngine();
                                        String targetLanguage = translateSetting.getTarget();
                                        String content = (String) contextParam.get("translatedContent");
                                        if ("youdao".equals(engine)) {
                                            if (YoudaoUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Chinese_China);
                                            } else if (YoudaoUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.English_GreatBritain);
                                            } else if (YoudaoUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Japanese);
                                            } else if (YoudaoUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Korean);
                                            }
                                        } else if ("baidu".equals(engine)) {
                                            if (BaiduUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Chinese_China);
                                            } else if (BaiduUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.English_GreatBritain);
                                            } else if (BaiduUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Japanese);
                                            } else if (BaiduUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Korean);
                                            }
                                        }else if ("google".equals(engine)) {
                                            if (GoogleUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Chinese_China);
                                            } else if (GoogleUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.English_GreatBritain);
                                            } else if (GoogleUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Japanese);
                                            } else if (GoogleUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(content, Languages.Korean);
                                            }
                                        }
                                    });
                                } else if ("textBoxSpeckMenuAfterTranslateMenuSelected".equals(c3.getName())) {
                                    ((JMenuItem) c3).addActionListener(e -> {
                                        String selectedText = jta.getSelectedText();
                                        String engine = translateSetting.getEngine();
                                        String targetLanguage = translateSetting.getTarget();

                                        if ("youdao".equals(engine)) {
                                            if (YoudaoUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Chinese_China);
                                            } else if (YoudaoUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.English_GreatBritain);
                                            } else if (YoudaoUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Japanese);
                                            } else if (YoudaoUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Korean);
                                            }
                                        } else if ("baidu".equals(engine)) {
                                            if (BaiduUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Chinese_China);
                                            } else if (BaiduUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.English_GreatBritain);
                                            } else if (BaiduUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Japanese);
                                            } else if (BaiduUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Korean);
                                            }
                                        } else if ("google".equals(engine)) {
                                            if (GoogleUtils.CHINESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Chinese_China);
                                            } else if (GoogleUtils.ENGLISH.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.English_GreatBritain);
                                            } else if (GoogleUtils.JPANESE.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Japanese);
                                            } else if (GoogleUtils.KOREAN.equals(targetLanguage)) {
                                                TTSUtils.speak(selectedText, Languages.Korean);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }
                }
                if ("textBoxKeyMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        KeySettingBuilder keySettingBuilder = (KeySettingBuilder) contextParam
                                .get("keySettingBuilder");
                        KeySettingFrame keySettingFrame = keySettingBuilder.getKeySettingFrame();
                        if (keySettingFrame == null) {
                            keySettingBuilder.build();
                        } else {
                            keySettingFrame.requestFocus();
                        }

                    });
                }
                if ("textBoxMinimumMenuItem".equals(c.getName())) {
                    ((JMenuItem) c).addActionListener(e -> {
                        JFrame textBoxFrame = (JFrame) textBoxComponents.get("textBoxFrame");
                        textBoxFrame.setState(JFrame.ICONIFIED);
                    });
                }
                if ("textBoxHelpMenu".equals(c.getName())) {
                    JMenu helpMenu = (JMenu) c;
                    Component[] menuComponents = helpMenu.getMenuComponents();
                    for (Component c2 : menuComponents) {
                        if ("textBoxHelpTopicMenuItem".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> System.out.println("topic"));
                        } else if ("textBoxAboutMenuItem".equals(c2.getName())) {
                            ((JMenuItem) c2).addActionListener(e -> System.out.println("about"));
                        }
                    }
                }

            }
        }
    }

    // 删除索引库内容
    private void deleteIndexAll() {
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
        try {
            IndexWriter indexWriter = new IndexWriter(fsdir, indexWriterConfig);
            indexWriter.deleteAll();
            indexWriter.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public void setJPopupMenu(JPopupMenu jpm) {
        this.jpm = jpm;
    }

}
